<?php

namespace app\model;

//use support\Model;
use app\controller\wmapi\IndexController;
use support\Db;
use Illuminate\Database\Eloquent\Model;

class WaOrdersOperateLog
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_orders_operate_log';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    public static function all($condition)
    {
        $list = Db::table('wa_orders_operate_log')
            ->select('*')
            ->where($condition)
            ->get();
        return json_encode($list);
    }

    public static function created($data){
        $result = Db::table('wa_orders_operate_log')->insertGetId($data);
        return $result;
    }

    public static function updated($data,$where){
        $result = Db::table('wa_orders_operate_log')->where($where)->update($data);
        return $result;
    }

    public static function geted($file,$where){
        $result = Db::table('wa_orders_operate_log')->select($file)->where($where)
            ->get();
        return json_encode($result);
    }

    public static function find($file,$where){
        $res = Db::table('wa_orders_operate_log')->select($file)->where($where)
            ->first();
        if ($res){
            if ($res->payment_voucher){
                $res->payment_voucher = json_decode($res->payment_voucher);
            }
            if ($res->voucher){
                $res->voucher = json_decode($res->voucher);
            }
        }
        return json_encode($res);
    }

    public static function delete($where){
        $result = Db::table('wa_orders_operate_log')->where($where)->delete();
        return $result;
    }

    public static function getWhereIn($file,$where,$whereIn){
        $list = Db::table('wa_orders_operate_log')
            ->select($file)
            ->where($where)
//            ->whereIn('status',$whereIn)
            ->whereIn('type',$whereIn)
            ->get();
        if ($list){
            // 订单状态 0 草稿 1 待核验(通过2 审批中(创建审批) / 不通过0 撤回草稿)   2审批中   3 待签署销售合同   4 待支付定金  5 订单采购中 6待支付余款 7待发货 8待收货 9已完成 10 已作废 11 删除
            foreach ($list as $k){

                // 类型 1 定金 2 余款
                if ($k->type == 1){
                    $k->type_name = '定金';
                }elseif ($k->type == 2){
                    $k->type_name = '余款';
                }else{
                    $k->type_name = '';
                }

//订单状态 0 草稿 1待核验   2审批中   3 待签署销售合同   4 待支付定金 12 财务确认到账 5 订单采购中  13 报关 7待发货   6待支付余款 16 待财务确认余款到账  8待收货  9 已完成 (交付提单/交车) 10 已作废 11 删除 17 待客户确认签署合同 18 审批驳回

//                if ($k->status == 0){
//                    $k->status_name = '草稿';
//                }elseif ($k->status == 1){
//                    $k->status_name = '待核验';
//                }elseif ($k->status == 2){
//                    $k->status_name = '审批中';
//                }elseif ($k->status == 3){
//                    $k->status_name = '待生成销售合同';
//                }elseif ($k->status == 4){
//                    $k->status_name = '待支付定金';
//                }elseif ($k->status == 5){
//                    $k->status_name = '订单采购中';
//                }elseif ($k->status == 6){
//                    $k->status_name = '待支付余款';
//                }elseif ($k->status == 7){
//                    $k->status_name = '待发货';
//                }elseif ($k->status == 8){
//                    $k->status_name = '待收货';
//                }elseif ($k->status == 9){
//                    $k->status_name = '已完成';
//                }elseif ($k->status == 10){
//                    $k->status_name = '已作废';
//                }elseif ($k->status == 11){
//                    $k->status_name = '删除';
//                }elseif ($k->status == 12){
//                    $k->status_name = '财务确认到账';
//                }elseif ($k->status == 13){
//                    $k->status_name = '报关';
//                }elseif ($k->status == 16){
//                    $k->status_name = '待财务确认余款到账';
//                }elseif ($k->status == 17){
//                    $k->status_name = '待客户确认签署';
//                }elseif ($k->status == 18){
//                    $k->status_name = '审批驳回';
//                }else{
//                    $k->status_name = '';
//                }

                $k->status_name = IndexController::saleOrderStatusName($k->status);
                if ($k->payment_voucher){
                    $k->payment_voucher = json_decode($k->payment_voucher);
                }


            }
        }
        return json_encode($list);
    }

}